import {
  request
} from "./index"
/** 创建订单
 * 
 * @param {string} way 
 * @param {string} token 
 */
const createTrade = (way, token) => {
  return request.post("v1/app/order/carts/create/trade", {
    client: 'WECHAT_MP',
    way
  }, {
    'Authorization': token
  })
}

/** 修改地址
 * 
 * @param {string} shippingAddressId 
 * @param {string} way 
 * @param {string} token
 */
const shippingAddress = (shippingAddressId, way, token) => {
  return request.get("v1/app/order/carts/shippingAddress", {
    shippingAddressId,
    way
  }, {
    'Authorization': token
  })
}

/** 获取订单
 * 
 * @param {number} page 
 * @param {number} status 
 * @param {string} token
 */
const getOrders = (page, status, token) => {
  let orderStatus = ''
  console.log(status)
  switch (status) {
    case 0:
      // 待付款
      orderStatus = 'UNPAID'
      break;
    case 1:
      // 待发货
      orderStatus = 'UNDELIVERED'
      break;
    case 2:
      // 已发货
      orderStatus = 'DELIVERED'
      break;
    case 3:
      // 已收货
      orderStatus = 'COMPLETED'
      break;
    default:
      break;
    case 4:
      // 售后
      return request.get("v1/app/order/carts/page", {
        page
      }, {
        'Authorization': token
      })
  }
  return request.get("v1/app/order", {
    page,
    orderType: 'NORMAL',
    orderStatus,
  }, {
    'Authorization': token
  })
}

/** 获取申请售后订单
 * 
 * @param {number} page 
 * @param {string} token 
 */

const getPostSaleOrder = (page, token) => {
  return request.get("v1/app/order/carts/page", {
    page
  }, {
    'Authorization': token
  })
}


/** 获取申请售后页面信息
 * 
 * @param {string} sn 
 * @param {string} token 
 */

const getAfterSaleInfo = (sn, token) => {
  return request.get(`v1/app/order/carts/applyAfterSaleInfo/${sn}`, {}, {
    'Authorization': token
  })
}

/** 退货
 * 
 * @param {string} orderSn 
 * @param {string} reason 
 * @param {string} token 
 */

const refund = (orderSn, reason, token) => {
  return request.post(`v1/app/order/carts/saveOrder/${orderSn}`, {
    reason,
    serviceType: 'RETURN_MONEY',
    refundWay: 'ORIGINAL',
    accountType: 'WECHAT'
  }, {
    'Authorization': token,
    'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
  })
}

/** 取消订单
 * 
 * @param {string} orderSn 
 * @param {string} reason 
 * @param {string} token 
*/

const removeOrder = (orderSn, reason, token) => {
  return request.post(`v1/app/order/${orderSn}/cancel`, {
    reason
  }, {
    'Authorization': token,
    'content-type': 'application/x-www-form-urlencoded'
  })
}

/** 获取支付参数
 * 
 * @param {string} sn 
 * @param {string} token 
 */
const getPayParam = (sn, token) => {
  return request.get(`v1/app/payment/cashier/pay/WECHAT/MP`, {    
		clientType:'WECHAT_MP',
    orderType: 'ORDER',
    sn
  }, {
    'Authorization': token
  })
}

/** 查询物流轨迹
 * 
 * @param {string} orderSn 
 * @param {string} token 
 */

const getTraces = (orderSn, token) => {
  return request.post(`v1/app/order/getTraces/${orderSn}`, {}, {
    'Authorization': token,
    'content-type': 'application/x-www-form-urlencoded'
  })
}

/** 确认收货
 * 
 * @param {string} orderSn 
 * @param {string} token 
 */

const receiving = (orderSn, token) => {
  return request.post(`v1/app/order/${orderSn}/receiving`, {}, {
    'Authorization': token,
    'content-type': 'application/x-www-form-urlencoded'
  })
}

/** 获取订单详情
 * 
 * @param {string} orderSn 
 * @param {string} token 
 */
const getOrderDetail = (orderSn, token) => {
  return request.get(`v1/app/order/${orderSn}`, {}, {
    'Authorization': token
  })
}

export {
  createTrade,
  shippingAddress,
  getOrders,
  getPayParam,
  getAfterSaleInfo,
  refund,
  removeOrder,
  getTraces,
  receiving,
  getOrderDetail,
  getPostSaleOrder
}